import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:test/locale/global_cupertino_localizations_delegate.dart';
import 'package:test/locale/global_material_localizations_delegate.dart';
import 'package:test/network/dio_helper.dart';
import 'package:test/providers/theme_provider.dart';
import 'package:test/theme/dark_theme.dart';
import 'package:test/theme/light_theme.dart';
import 'package:test/pages/home/index.dart';
import 'package:test/pages/user/info.dart' deferred as userInfo;

void main() {
  runApp(ProviderScope(child: const MyApp()));
}

var theme = 'light';

class MyApp extends ConsumerWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    DioHelper.init(ref);
    final theme = ref.watch(themeProvider);
    return MaterialApp(
      title: 'Flutter Demo',
      theme: theme == 'light' ? lightTheme : darkTheme,
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
      localizationsDelegates: [
        GlobalMaterialLocalizationsDelegate(),
        GlobalCupertinoLocalizationsDelegate(),
        DefaultWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        Locale('en', 'US'), // 英语（美国）
        Locale('zh', 'CN'), // 中文（中国）
      ],
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  final List<Widget> _pages = [HomeIndexPage(), SizedBox.shrink()];
  final List<Future<Widget> Function()> _pageFutures = [
    () async {
      await userInfo.loadLibrary();
      return userInfo.UserInfoPage();
    },
  ];

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) async {
          if (_pages[index] is SizedBox) {
            _pages[index] = await _pageFutures[index - 1]();
          }
          setState(() {
            _currentIndex = index;
          });
        },
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
    );
  }
}
